package com.lagou.controller;

import com.lagou.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 注册接⼝， 0:注册成功， 1:注册码已过期  2:注册失败
     *
     * @param email
     * @param password
     * @param code
     * @return
     */
    @RequestMapping("/register/{email}/{password}/{code}")
    public String register(@PathVariable("email") String email, @PathVariable("password") String password, @PathVariable("code") String code, HttpServletResponse response) {
        return userService.register(email, password, code, response);
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
     *
     * @param email
     * @return
     */
    @RequestMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable("email") String email) {
        return userService.isRegistered(email);
    }

    /**
     * 登录接⼝，验证⽤用户名密码合法性，根据⽤用户名和密码⽣生成token， token存⼊入数据库，
     * 并写⼊入cookie中，登录成功返回邮箱地址，重定向到欢迎⻚页
     *
     * @param email
     * @param password
     * @return
     */
    @RequestMapping("/login/{email}/{password}")
    public Boolean login(@PathVariable("email") String email, @PathVariable("password") String password, HttpServletResponse response) {
        return userService.login(email, password, response);
    }

    /**
     * 根据token查询⽤用户登录邮箱接⼝
     *
     * @param token
     * @return
     */
    @RequestMapping("/info/{token}")
    public String info(@PathVariable("token") String token) {
        return userService.info(token);
    }
}
